<%@ page language="java" session="true" errorPage="ElderhealthError.jsp"%>
<%@ page import="com.elderhealth.worker.HomecareWorker"%>
<%
/*
 * Source:   ViewWorker.jsp
 * Purpose:  This page will pull the corrent Worker out of the already passed
 *           worker objects and display their attributes to the screen.
 */
%>

<html>
<head>
<link rel="stylesheet" type="text/css" href="elderhealth.css">
<!-- Scripting Portion -->
<script type="text/javascript" src="elderhealth.js"></script>
</head>
<body>

<%@ include file="ElderhealthHeader.jsp" %>

<%
/* Intialize variables */
String errorCode;
String errorMessage;
String redirectURL;

try
{
  /* Retrieve worker id from the passed parameters */
  int selectedWorkerID = Integer.parseInt(request.getParameter("selectedWorker"));

  /* Retrieve workers and present them */
  HomecareWorker[] workerResults = (HomecareWorker[])session.getAttribute("results"); 
%>

<table border=0 align="left" border=0 cellpadding=0 cellspacing=0 width="600">
    <tr class="contactHeaderText"><td height="18" align="left" valign="middle" style="PADDING-LEFT:10px">View Worker</td></tr>
    <tr><td class="layoutLine" valign="middle" align="left" width="302" height="1" colSpan="4"></td></tr>
    <tr class="contactBar1">
	  <td align="left" valign="middle" style="PADDING-LEFT:10px">

<%
/* Check to make sure the workers from the search are still part of the session variable */
if (workerResults == null || workerResults.length == 0)
{
  errorCode = "1";
  errorMessage = "Error occurred in ViewWorker.jsp. No workers were passed to this page from the search page." +
                   "Probably a problem with the session. Please retry the search.";
  redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
  response.sendRedirect(redirectURL);
} 
else
{
  /* Initialize the worker variable */
  HomecareWorker selectedWorker = new HomecareWorker();
  
  /* Pull the correct worker from the list passed in*/
  for (int i = 0; i < workerResults.length; i ++)
  {
    if (workerResults[i].getWorkerId() == selectedWorkerID) 
	{
	   selectedWorker = workerResults[i];
	}
  }
  
  /* Make sure that the worker was found in the passed list */
  if (selectedWorker.getWorkerId() == -1) 
  {
    errorCode = "1";
    errorMessage = "Error occurred in ViewWorker.jsp. Selected worker was not in list of workers passed to this page." +
                   "Probably a problem with the session. Please retry the search.";
    redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
    response.sendRedirect(redirectURL);
  }
  else
  {
    /* Worker was found, display details */
%>
    Worker ID: <%=selectedWorker.getWorkerId()%><br>
	First Name: <%=selectedWorker.getFirstName()%><br>
    Last Name: <%=selectedWorker.getLastName()%><br>

	<form name="workerSearch" action="GetWorkerSchedule.jsp?workerID=selectedWorker.getWorkerId()" method="get">
	  <input type="hidden" name="workerID" value=<%=selectedWorkerID%>>
	  <input type="button" value="View Schedule" onClick="submit()">
	  <input type="button" value="Edit Worker" onclick="top.location='HomecareWorkerManagement.jsp'">
	  <input type="button" value="Go Back" onClick="history.back();"><br><br>
	</form>
<%
  }
 }
} 
catch (Exception e) 
{
    errorCode = "1";
    errorMessage = "Error occurred in ViewWorker.jsp. Most likely error is that no worker id was submitted to this page. Error is:" +
                   e.toString();
    redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
    response.sendRedirect(redirectURL);
}
%>
	<br>
    </td></tr>
    <tr><td class="layoutLine" valign="middle" align="left" width="302" height="1" colSpan="4"></td></tr>
</table>

</body>
</html>